<?php
require_once 'Zend/Auth.php';

/**
 * Storage container for all user information.
 * 
 * @author Florian Sonner
 */
class Translator_User
{	
	/**
	 * Group value of authenticated users.
	 * @var int
	 */
	const GROUP_AUTH = 1;
	
	/**
	 * Group value of users with translation privileges.
	 * @var int
	 */
	const GROUP_TRANSLATOR = 2;
	
	/**
	 * Group value of administrators.
	 * @var int
	 */
	const GROUP_ADMIN = 3;
	
	/**
	 * @var Zend_Auth
	 */
	private $_auth;
	
	/**
	 * User information. 
	 * @var array
	 */
	private $_data;
	
	/**
	 * Try to  authenticate the user.
	 */
	public function __construct() {
		$this->_auth = Zend_Auth::getInstance();
		
		// gather user profile in our database
		if($this->_auth->hasIdentity()) {
			require_once 'models/UserModel.php';
			$userModel = new UserModel();
			
			$this->_data = $userModel->fetch($this->_auth->getIdentity());
		}
	}
	
	/**
	 * Try to authenticate using forum credentials.
	 * 
	 * @param string $username username
	 * @param string $password password
	 * @return Zend_Auth_Result Result of {@link Zend_Auth_Adapter_DbTable#authenticate()) 
	 */
	public function signIn($username, $password) {		
		require_once 'GeoGebra/Auth/Adapter/Forum.php';

		$authAdapter = new GeoGebra_Auth_Adapter_Forum($username, $password);		
		$result = $this->_auth->authenticate($authAdapter);
		
		if (!$result->isValid()) {
			$this->_auth->clearIdentity();
		} else {
			require_once 'models/UserModel.php';
			$userModel = new UserModel();
			
			// try to fetch user data
			$userrow = $userModel->fetch($this->_auth->getIdentity());
			
			// create record if there is none at the moment
			if(empty($userrow)) {
				$userdata = $authAdapter->getData();
				$userModel->create($this->_auth->getIdentity(), $userdata['email']);
			}
		}

		return $result;
	}
	
	/**
	 * Sign out. Calls {@link Zend_Auth#clearIdentity()}.
	 */
	public function signOut() {
		$this->_auth->clearIdentity();
	}
	
	/**
	 * @param string $field
	 */
	public function __get($field) {
		return $this->_data->$field;
	}
	
	/**
	 * @param string $field
	 */
	public function __isset($field) {
		return isset($this->_data->$field);
	}
	
	/**
	 * @return bool true if the user is signed in, false otherwise
	 */
	public function isAuthenticated() {
		return $this->_auth->hasIdentity();
	}
	
	/**
	 * @return bool true if the user is an administrator
	 */
	public function isAdministrator() {
		return $this->isAuthenticated() 
			&& $this->_data->group == self::GROUP_ADMIN;
	}
	
	/**
	 * Wrapper for {@see #isAdministrator()}
	 */
	public function isAdmin() {
		return $this->isAdministrator();
	}
	
	/**
	 * @param bool $pending
	 * @return bool true if the user is a translator. If the parameter
	 * 'pending' is set to true this method will also return true if the
	 * user is requested translation privileges.  
	 */
	public function isTranslator($pending = false) {
		if(!$this->isAuthenticated()) {
			return false;
		}
		
		if($pending) {
			return $this->_data->group >= self::GROUP_TRANSLATOR || $this->_data->request;
		} else {
			return $this->_data->group >= self::GROUP_TRANSLATOR;
		}
	}
	
	/**
	 * Check whether the user can translate the given language.
	 * 
	 * @param $languageid
	 */
	public function canTranslate($languageid) {
		if($this->isAdministrator()) {
			return true;
		}
		
		if($this->isAuthenticated()) {
			require_once 'models/UserModel.php';
			$userModel = new UserModel();
			
			return $userModel->canTranslate($this->id, $languageid);
		}
		
		return false;
	}
}